{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from selenium import webdriver \n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "opts = webdriver.ChromeOptions()\n",
    "opts.add_argument('--no-sandbox')\n",
    "opts.add_argument('window-size=1920x3000') \n",
    "opts.add_argument('--disable-gpu') \n",
    "opts.add_argument('--hide-scrollbars') \n",
    "#opts.add_argument('blink-settings=imagesEnabled=false') \n",
    "driver = webdriver.Chrome( options = opts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "from urllib.parse import urljoin\n",
    "from uuid import uuid4\n",
    "import requests\n",
    "import base64\n",
    "from PIL  import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def base64_api(uname, pwd, img, typeid):\n",
    "    with open(img, 'rb') as f:\n",
    "        base64_data = base64.b64encode(f.read())\n",
    "        b64 = base64_data.decode()\n",
    "    data = {\"username\": uname, \"password\": pwd, \"typeid\": typeid, \"image\": b64}\n",
    "    result = json.loads(requests.post(\"http://api.ttshitu.com/predict\", json=data).text)\n",
    "    if result['success']:\n",
    "        return result[\"data\"][\"result\"]\n",
    "    else:\n",
    "        return result[\"message\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cut_picture(picture_path):\n",
    "    srcPath = picture_path+\".jpg\"\n",
    "    dstPath = picture_path+\".png\"\n",
    "\n",
    "    \n",
    "    img_1 = Image.open(srcPath)\n",
    "    \n",
    "    crop_box = (486,354,750,415)\n",
    "   \n",
    "    img_2 = img_1.crop(crop_box)\n",
    "    img_2.save(dstPath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已经进入验证码阶段\n",
      "《数据分析与知识发现》期刊征文----没有下载权限\n",
      "已经进入验证码阶段\n",
      "《数字图书馆论坛》征稿启事----没有下载权限\n",
      "已经进入验证码阶段\n",
      "大数据挖掘技术下的企业产权平等保护----没有下载权限\n",
      "已经进入验证码阶段\n",
      "教育管理研究的范式问题与方法变革:一个技术倡议----没有下载权限\n",
      "已经进入验证码阶段\n",
      "基于数据挖掘郑新教授治疗慢性肾衰竭组方用药规律----没有下载权限\n",
      "已经进入验证码阶段\n",
      "基于Python语言的藏医对治“年壬（”瘟疫）方剂数据库构建及配伍规律分析----没有下载权限\n",
      "已经进入验证码阶段\n",
      "中国科学院资源与环境信息系统国家重点实验室----没有下载权限\n",
      "已经进入验证码阶段\n",
      "厂网协调模式下燃煤机组发电能力评测方法研究----没有下载权限\n",
      "已经进入验证码阶段\n",
      "基于数据挖掘的飞行器气动布局设计知识提取----没有下载权限\n",
      "已经进入验证码阶段\n",
      "基于中药复方干预湿热证2型糖尿病临床研究分析中药应用规律----没有下载权限\n",
      "基于数据挖掘的电针治疗骨质疏松症腧穴谱及刺激参数研究----没有下载权限\n",
      "已经进入验证码阶段\n",
      "基于数据挖掘针灸治疗面瘫用穴规律研究----没有下载权限\n",
      "神经网络导向的形态分析与设计决策支持方法探索----没有下载权限\n",
      "基于距离和权重改进的K-means算法----没有下载权限\n",
      "烟草近红外光谱分析网络化及其应用进展----没有下载权限\n",
      "MOOC学习者在线学习行为和学习绩效评估模型研究----没有下载权限\n",
      "数据挖掘对个人信息的侵害与保护路径----没有下载权限\n",
      "基于关联规则算法的推荐方法研究综述----没有下载权限\n",
      "E-Science环境下iSchools联盟院校“科学数据”课程调查与思考----没有下载权限\n",
      "面向灾害预警的空间数据概念增强----没有下载权限\n",
      "基于数据挖掘的城轨系统电能消耗模式分析----没有下载权限\n",
      "基于数据挖掘的《外科正宗·痔疮主治方》用药规律分析----没有下载权限\n",
      "基于数据挖掘中药治疗卒中后认知障碍用药规律探析----没有下载权限\n",
      "基于教育视频日志的多维情境模型构建----没有下载权限\n",
      "基于时空数据的城市人流移动模式挖掘----没有下载权限\n",
      "基于生成对抗网络的差分隐私数据发布方法----没有下载权限\n",
      "网络空间安全战略情报保障能力的影响因素及对策研究----没有下载权限\n",
      "基于数据挖掘分析软组织肉瘤中PRC1的表达与意义及其对肉瘤细胞生长的影响----没有下载权限\n",
      "基于AR＿TSM的时间序列motif关联规则挖掘方法研究----没有下载权限\n",
      "管理信息系统中自适应导航模型的构建----没有下载权限\n",
      "基于距离与误差平方和的差分隐私K-means聚类算法----没有下载权限\n",
      "长江经济带水质目标管理平台总体设计----没有下载权限\n",
      "基于属性分类的装备保障数据特征挖掘法----没有下载权限\n",
      "在线评论有用性的深度数据挖掘——基于TripAdvisor的酒店评论数据----没有下载权限\n",
      "多维度特征融合的教学行为智能分析模式构建----没有下载权限\n",
      "基于继电保护装置态势感知及辅助决策的智能运维系统----没有下载权限\n",
      "利用基本信息和行为数据发现高校贫困学生----没有下载权限\n",
      "基于PPDM的智慧图书馆用户隐私数据保护研究----没有下载权限\n",
      "《电子科技大学学报》征订启事----没有下载权限\n",
      "基于图卷积神经网络的位置语义推断----没有下载权限\n",
      "基于数据挖掘的电网用户行为分析----没有下载权限\n",
      "信息化时代反恐情报工作的创新、挑战与启示:基于对美国的分析----没有下载权限\n",
      "无人车大数据与云控制技术综述                                                                     撤回----没有下载权限\n",
      "基于关联规则结合贝叶斯网络的症状间关系及其对证候诊断贡献度的研究：以新型冠状病毒肺炎湿热蕴肺证为例----没有下载权限\n",
      "基于数据挖掘探讨谢春光治疗糖尿病肾病组方规律----没有下载权限\n",
      "基于中医传承辅助平台探讨中医药治疗支气管扩张的证治规律----没有下载权限\n",
      "《数据分析与知识发现》期刊征文----没有下载权限\n",
      "《数字图书馆论坛》征稿启事----没有下载权限\n",
      "大数据挖掘技术下的企业产权平等保护----没有下载权限\n",
      "教育管理研究的范式问题与方法变革:一个技术倡议----没有下载权限\n"
     ]
    }
   ],
   "source": [
    "data = pd.read_excel(\"CNKI_数据挖掘.xlsx\")\n",
    "base = \"https://kns.cnki.net/\"\n",
    "count = 450\n",
    "end_count = 500\n",
    "title_list = data[\"标题\"].to_list()[count:end_count]\n",
    "download_url_list = data[\"链接\"].to_list()[count:end_count]\n",
    "for title,download_url in zip(title_list,download_url_list):\n",
    "    try:\n",
    "        driver.get(download_url)\n",
    "        time.sleep(0.5)\n",
    "        js = 'document.getElementById(\"pdfDown\").setAttribute(\"target\",\"\");'\n",
    "        driver.execute_script(js)\n",
    "        time.sleep(0.5)\n",
    "        driver.find_element_by_xpath('//li[@class=\"btn-dlpdf\"]').click()\n",
    "        time.sleep(0.5)\n",
    "        source = driver.page_source\n",
    "        if \"安全验证\" in source: \n",
    "            print(\"已经进入验证码阶段\")\n",
    "            time.sleep(2)\n",
    "            url_path = re.findall('<dt><img id=\"vImg\" src=\"(.*?)\" alt=\"验证码\" title=\"点击切换验证码\"></dt>',source)[0] \n",
    "            picture_url = urljoin(base,url_path)\n",
    "            content = requests.get(picture_url).content\n",
    "            picture_name = f\"./code/{uuid4()}\"\n",
    "            driver.get_screenshot_as_file(picture_name+\".jpg\")\n",
    "            cut_picture(picture_name)\n",
    "            result = base64_api(uname='#####省去', pwd='#####省去', img=picture_name+\".png\", typeid=3)\n",
    "            print(\"识别结果为：\",result) \n",
    "            driver.find_element_by_id('vcode').send_keys(result) \n",
    "            time.sleep(5)\n",
    "            driver.find_element_by_xpath('/html/body/div/form/dl/dd/button').click() \n",
    "    except:\n",
    "        print(f'{title}----没有下载权限')\n",
    "        continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
